# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <math.h>


// verifica se um numero eh primo usando o Clivo de Eratostenes
/*
int is_prime ( int n)
{
	char *list = NULL;
	char *p;
	
	unsigned int i, j, k; 
	unsigned int r2n;
	
	int r;
	
	list = (char *) calloc(n + 1, sizeof(char));
	
	printf("\n %p", list);
	
	if (list == NULL)
	{
		printf("\n Erro ao alocar memória!");
		exit(1);
	}
	printf("\n A!");
	
	for (i = 0; i <= n; i++)
		printf(" %c", *(list + i));
	
	list = (char *) memset(list, '1', n + 1);
	
	printf("\n B!");
	
	//p = list;
	
	//*p = '0';
	//*(p+1) = '0';
	
	printf("\n C!");
	
	for (i = 0; i <= n; i++)
		printf("%2c", *(list + i));
	
	r2n = (unsigned int) ceil(sqrt(n));
	
	for (i = 2; i <= r2n; i++)
	{
		p = list + i;
		
		if (*p == '0')
			continue;
			
		j = (unsigned int) pow(i, 2);
		
		for (k = 0, j = j + k * i; j <= n; k++)
			*(p + j) = '0';
			
		if ( *(list + n) == '0')
			return -1;
	}
	if ( *(list + n) == '0') 
		r = -1; 
	else r = 1;
	
	for (i = 0; i <= n; i++)
		printf("%2c", *(list + i));
	
	free(list);
	
	return r;	
}
*/

int main (int argc, char *argv[])
{
	unsigned int n = 21;
	
	char *list = NULL;
	char *p;
	
	unsigned int i, j, k; 
	unsigned int r2n;
	
	int r;
	
	list = (char *) calloc(n + 1, sizeof(char));
	
	//printf("\n %p", list);
	
	if (list == NULL)
	{
		printf("\n Erro ao alocar memória!");
		exit(1);
	}
	
	printf("\n");
	for (i = 0; i <= n; i++)
		printf("%c.", *(list + i));
	
	list = (char *) memset(list, '1', n + 1);
	
	p = list;
	
	*p = '0'; 
	*(p+1) = '0';
	
	printf("\n");
	for (i = 0; i <= n; i++)
		printf("%c.", *(list + i));
	
	r2n = (unsigned int) sqrt(n);
	
	printf("\n %u", r2n);
	
	for (i = 2; i <= r2n; i++)
	{
		p = (list + i);
		
		if (*p == '0')
			continue;
			
		k = 0;
		
		do
		{	
			j = (unsigned int) pow(i, 2) + k * i;
			
			*(list + j) = '0';
			
			k++;
		} 
		while (j <= n);
			
		//if (*(list + n) == '0')
			//return -1;
	}
	if (*(list + n) == '0') 
		r = -1; 
	else r = 1;
	
	printf("\n %d \n", r);
	for (i = 0; i <= n; i++)
		printf("%c.", *(list + i));
	
	free(list);
	
	return EXIT_SUCCESS;
}
